﻿/**
 * PvZ Tool
 * 
 * @classname   Tool
 * @description Code for main window form
 * @author      Martomo
 * @author      Kamizoom
 * @version     0.0.1
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PvZTool
{

    public partial class Tool : Form
    {

        public Tool()
        {
            InitializeComponent();
        }

        private void LoadTool(object sender, EventArgs e)
        {
            this.selectLevel.Items.AddRange(Offset.arrayLevelName);
        }

        private void ExitTool(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void AboutTool(object sender, EventArgs e)
        {
            Function.MsgBox(Program.appName +" v"+ Program.appVersion+"\ncreated by Martomo and Kamizoom", "About");
        }

        private void OpenSave(object sender, EventArgs e)
        {
            if (Save.Open() == true)
            {
                LoadValues();
                ChangeMenuState("close");
            }
        }

        private void CloseSave(object sender, EventArgs e)
        {
            Save.Close();
            ResetValues();
            ChangeMenuState("open");
        }

        private void WriteSave(object sender, EventArgs e)
        {
            if (Save.Write() == true)
            {
                Function.MsgBox("Game has been saved", "Saved");
            }
        }

        private void ChangeMenuState(string status = "open")
        {
            switch (status)
            {
                case "close":
                    this.OpenItem.Text = "&Close";
                    this.SaveItem.Enabled = true;
                    this.OpenItem.Click -= new System.EventHandler(this.OpenSave);
                    this.OpenItem.Click += new System.EventHandler(this.CloseSave);
                    break;
                case "open":
                default:
                    this.Text = Program.appName;
                    this.OpenItem.Text = "&Open";
                    this.SaveItem.Enabled = false;
                    this.OpenItem.Click -= new System.EventHandler(this.CloseSave);
                    this.OpenItem.Click += new System.EventHandler(this.OpenSave);
                    break;
            }
        }

        private void LoadValues()
        {
            this.selectLevel.Enabled = true;
            // Current Level in save
            Debug.WriteLine("Level: " + Save.GetValue(Offset.posLevel));
            // Get index from arrayLevelValue
            int indexLevel = Array.IndexOf(Offset.arrayLevelValue, Save.GetValue(Offset.posLevel));
            // Use index to retrieve value from arrayLevelName
            this.selectLevel.SelectedItem = Offset.arrayLevelName[indexLevel];
            Debug.WriteLine("Value: " + Offset.arrayLevelName[indexLevel]);
        }

        private void ResetValues() {
            this.selectLevel.SelectedIndex = -1;
            this.selectLevel.Text = "";
            this.selectLevel.Enabled = false;
        }

        private void ChangeLevel(object sender, EventArgs e)
        {
            Debug.WriteLine("LevelChange");
            Debug.WriteLine("Index: " + selectLevel.SelectedIndex);
            Debug.WriteLine("Item: " + selectLevel.SelectedItem);
            if (selectLevel.SelectedIndex > -1)
            {
                // Get index from arrayLevelName
                int indexLevel = Array.IndexOf(Offset.arrayLevelName, selectLevel.SelectedItem);
                // Use index to retrieve value from arrayLevelValue
                Debug.WriteLine("HEX value: " + Offset.arrayLevelValue[indexLevel]);

                // Store change in Save.saveChanges
                Save.SetChange(Offset.posLevel, Offset.arrayLevelValue[indexLevel]);
            }
        }
    }
}
